﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace MvcShop.Extensions.Html
{
    public class ParameterExtensions
    {
        /// <summary>
        /// 参数的值
        /// </summary>
        public object Value { get; set; }
        /// <summary>
        /// 参数的名称
        /// </summary>
        public string Name { get; set; }
    }

    public static class PagerXExtensions
    {
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="html"></param>
        /// <param name="page">当前页</param>
        /// <param name="total">总页数</param>
        /// <returns></returns>
        public static string PagerX(this HtmlHelper html, int page, int total)
        {
            return PagerX(html, page, total, BarStyle.technorati);
        }

        public static string PagerX(this HtmlHelper html, int page, int total, BarStyle style)
        {
            return PagerX(html, page, total, style, total);
        }

        public static string PagerX(this HtmlHelper html, int page, int total, BarStyle style, int show)
        {
            return PagerX(html, page, total, style, show, null);
        }

        public static string PagerX(this HtmlHelper html, int page, int total, BarStyle style, int show, Dictionary<string, object> parameters)
        {
            if (total == 1)
            {
                return "";
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                string _path = html.ViewContext.HttpContext.Request.Path;
                sb.Append("<div class=\"");
                sb.Append(style.ToString());
                sb.Append("\" >");

                string queryString = html.ViewContext.HttpContext.Request.QueryString.ToString();
                if (queryString.IndexOf("page=") < 0)
                {
                    queryString += "&page=" + page;
                }
                if (parameters != null)
                {
                    foreach (var par in parameters)
                    {
                        string findStr = par.Key + "=";
                        if (queryString.IndexOf(findStr) < 0)
                        {
                            queryString += "&" + par.Key + "=" + par.Value;
                        }
                    }
                }

                Regex re = new Regex(@"page=\d+", RegexOptions.IgnoreCase);
                string result = re.Replace(queryString, "page={0}");

                if (page != 1)
                {
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"第一页\">{1}</a></span>", _path + "?" + string.Format(result, 1), "<<");
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"上一页\">{1}</a></span>", _path + "?" + string.Format(result, page - 1), "<");
                }
                if (page > (show + 1))
                {
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"前" + (show + 1) + "页\">{1}</a></span>", _path + "?" + string.Format(result, page - (show + 1)), "..");

                }
                for (int i = page - show; i <= page + show; i++)
                {
                    if (i == page)
                    {
                        sb.AppendFormat("<span class=\"current\">{0}</span>", i);
                    }
                    else
                    {
                        if (i > 0 & i <= total)
                        {
                            sb.AppendFormat("<span><a href=\"{0}\">{1}</a></span>", _path + "?" + string.Format(result, i), i);
                        }
                    }
                }
                if (page < (total - (show)))
                {
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"后" + (show + 1) + "页\">{1}</a></span>", _path + "?" + string.Format(result, page + (show + 1)), "..");

                }
                if (page < total)
                {
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"下一页\">{1}</a></span>", _path + "?" + string.Format(result, page + 1), ">");
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"最后一页\">{1}</a></span>", _path + "?" + string.Format(result, total), ">>");

                }
                sb.AppendFormat("<span class=\"current\">共{1}页</span>", page, total);
                if (parameters != null)
                {

                }
                sb.Append("</div>");
                return sb.ToString();
            }
        }

        public static string PagerX(this HtmlHelper html, int page, int total, BarStyle style, int show, Dictionary<string, object> parameters, string first, string last, string next, string previous,string action,string keyword)
        {
            if(string.IsNullOrEmpty(first))
            {
                first = "<<";
            }
            if (string.IsNullOrEmpty(last))
            {
                last = ">>";
            }
            if (string.IsNullOrEmpty(next))
            {
                next = ">";
            }
            if (string.IsNullOrEmpty(previous))
            {
                previous = "<";
            }
            if (total == 1)
            {
                return "";
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                string _path = html.ViewContext.HttpContext.Request.Path;
                sb.Append("<div class=\"");
                sb.Append(style.ToString());
                sb.Append("\" >");

                string queryString = html.ViewContext.HttpContext.Request.QueryString.ToString();
                if (queryString.IndexOf("page=") < 0)
                {
                    queryString += "&page=" + page;
                }

                if (parameters != null)
                {
                    foreach (var par in parameters)
                    {
                        string findStr = par.Key + "=";
                        if (queryString.IndexOf(findStr) < 0)
                        {
                            queryString += "&" + par.Key + "=" + par.Value;
                        }
                    }
                }

                Regex re = new Regex(@"page=\d+", RegexOptions.IgnoreCase);
                string result = re.Replace(queryString, "page={0}");

                if (page != 1)
                {
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"第一页\">{1}</a></span>", _path + "?" + string.Format(result, 1), first);
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"上一页\">{1}</a></span>", _path + "?" + string.Format(result, page - 1), previous);
                }
                if (page > (show + 1))
                {
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"前" + (show + 1) + "页\">{1}</a></span>", _path + "?" + string.Format(result, page - (show + 1)), "..");

                }
                for (int i = page - show; i <= page + show; i++)
                {
                    if (i == page)
                    {
                        sb.AppendFormat("<span class=\"current\">{0}</span>", i);
                    }
                    else
                    {
                        if (i > 0 & i <= total)
                        {
                            sb.AppendFormat("<span><a href=\"{0}\">{1}</a></span>", _path + "?" + string.Format(result, i), i);
                        }
                    }
                }
                if (page < (total - (show)))
                {
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"后" + (show + 1) + "页\">{1}</a></span>", _path + "?" + string.Format(result, page + (show + 1)), "..");

                }
                if (page < total)
                {
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"下一页\">{1}</a></span>", _path + "?" + string.Format(result, page + 1), next);
                    sb.AppendFormat("<span><a href=\"{0}\" title=\"最后一页\">{1}</a></span>", _path + "?" + string.Format(result, total), last);

                }
                sb.AppendFormat("<span class=\"total\">共{1}页</span>", page, total);
                sb.AppendFormat("<form id=\"filterPageForm\" name=\"filterPageForm\" method=\"get\" action=\"{0}\">", action);
                sb.AppendFormat("<input type=\"hidden\" name=\"keywords\" id=\"keywords\" value=\"{0}\"/>", keyword);
                if (parameters != null)
                {
                    foreach (var p in parameters)
                    {
                        sb.AppendFormat("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\"/>", p.Key, p.Value);

                    }
                }
                sb.AppendFormat("到第<input class=\"page\" type=\"text\" name=\"page\" id=\"page\" value=\"{0}\"/>页<input class=\"but\" type=\"submit\" value=\"确定\" />", page);
                sb.Append("</form>");
                sb.Append("</div>");
                return sb.ToString();
            }
        }
    }
}
